package com.zhang.family_doctor.service.impl;

import com.zhang.family_doctor.entity.Purview;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.mapper.PurviewMapper;
import com.zhang.family_doctor.service.PurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class PurviewServiceImpl implements PurviewService {

    @Autowired
    private PurviewMapper purviewMapper;

    /**
     * 新增权限
     * @param purview
     * @return
     */
    @Override
    public R<String> save(Purview purview) {

        int id = UUID.randomUUID().toString().replaceAll("-", "-").hashCode();
        id = id < 0 ? -id : id;

        purview.setId(id);
        purview.setCreateTime(new Date());
        purview.setUpdateTime(new Date());

        purviewMapper.save(purview);

        return R.success("新增成功！");
    }

    /**
     * 修改权限
     * @param purview
     * @return
     */
    @Override
    public R<String> update(Purview purview) {

        purview.setUpdateTime(new Date());

        purviewMapper.update(purview);

        return R.success("修改成功！");
    }

    /**
     * 查看所有的权限
     * @return
     */
    @Override
    public R<List<Purview>> getAll() {
        List<Purview> purviewList = purviewMapper.getAll();
        return R.success(purviewList);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @Override
    public R<String> deleteById(Integer id) {
        purviewMapper.deleteById(id);
        return R.success("删除成功！");
    }
}
